<!-- 新增/修改 -->
<div class="col-md-6 col-md-offset-3">
	<div class="panel panel-default">
		<div class="panel-body" style="padding-bottom: 0px;">
			<div class="panel-heading" style="text-align: center;">
				<div><i style="color: grey">生日输入农历即可，自动换算成公历。</i></div>
			</div>
			<form class="form form-horizontal" id="form_person">
				<input type="hidden" name="id" value="<%=person.id%>">
				<div class="form-group">
					<label for="name" class="col-sm-1 control-label">姓名</label>
					<div class="col-sm-10">
						<input type="text" class="form-control" name="name" value="<%=person.name%>" placeholder="请输入姓名" required>
					</div>
				</div>
				<div class="form-group">
					<label for="sex" class="col-sm-1 control-label">性别</label>
					<div class="col-sm-10">
						<label class="radio-inline" style="width: 60px;text-align: left;">
							<input type="radio" name="sex" value="M" <%=person.sex=='男'?'checked':''%> required> 男
						</label>
						<label class="radio-inline" style="width: 60px;text-align: left;">
							<input type="radio" name="sex"  value="F" <%=person.sex=='女'?'checked':''%> required> 女
						</label>
					</div>
				</div>
				<div class="form-group">
					<input type="hidden" name="nong" value="<%=person.nong%>">
					<label for="birthday" class="col-sm-1 control-label">农历生日</label>
					<div class="col-sm-10">
						<input type="text" style="width: 68px;text-align: center;" name="nongY" maxlength="4" value="<%=person.nongY||DT.today.nongY%>">年
						<select id="nongM" name="nongM">
							<% for (let i = 1; i <= 12; i++) { let yue=(person.nong||DT.today.nong).split('-')[1]; %>
								<option value="<%=i%>" <%=yue==i?'selected':''%>><%=DT.getYue(i)%>月</option>
							<% } %>
						</select>
						<select id="nongD" name="nongD">
							<% for (let i = 1; i <= nongDayCount; i++) { let ri=(person.nong||DT.today.nong).split('-')[2]; %>
								<option value="<%=i%>" <%=ri==i?'selected':''%>><%=DT.getRi(i)%></option>
							<% } %>
						</select>
					</div>
				</div>
				<div class="form-group">
					<input type="hidden" name="gong" value="<%=person.gong||DT.today.gong%>">
					<label for="birthday" class="col-sm-1 control-label">公历生日</label>
					<div class="col-sm-10">
						<input type="text" style="width: 48px;text-align: center;" name="gongY" value="<%=person.gongY||DT.today.gongY%>" disabled>年
						<input type="text" style="width: 40px;text-align: center;" name="gongM" value="<%=person.gongM||DT.today.gongM%>" disabled>月
						<input type="text" style="width: 40px;text-align: center;" name="gongD" value="<%=person.gongD||DT.today.gongD%>" disabled>日
						<input type="text" style="width: 54px;text-align: center;" name="gongW" value="<%=person.gongW||DT.today.gongW%>" disabled>
					</div>
				</div>
				<div class="form-group">
					<label for="fName" class="col-sm-1 control-label">父亲</label>
					<div class="col-sm-10">
						<select id="fid" name="fid" data-live-search="true">
							<option value="<%=person.f.id%>" selected><%=EJS.info(person.f)||'没有选中任何项'%></option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="mName" class="col-sm-1 control-label">母亲</label>
					<div class="col-sm-10">
						<select id="mid"  name="mid"data-live-search="true">
							<option value="<%=person.m.id%>" selected><%=EJS.info(person.m)||'没有选中任何项'%></option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="xName" class="col-sm-1 control-label">配偶</label>
					<div class="col-sm-10">
						<select id="xid" name="xid" data-live-search="true">
							<option value="<%=person.x.id%>" selected><%=EJS.info(person.x)||'没有选中任何项'%></option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<input type="hidden" name="nian" value="<%=person.nian||DT.today.nian%>">
					<label class="col-sm-1 control-label"></label>
					<div class="col-sm-10">
						<button type="submit" class="btn btn-primary">保存</button>
						<button type="cancel" class="btn btn-info">取消</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>

<script>
	// 汉字年转数字年
	function nongY2N(y) {
		return y.replace(/零/g,'0').replace(/一/g,'1').replace(/二/g,'2').replace(/三/g,'3').replace(/四/g,'4')
				.replace(/五/g,'5').replace(/六/g,'6').replace(/七/g,'7').replace(/八/g,'8').replace(/九/g,'9');
	}
	// 数字年转汉字年
	function nongN2Y(n) {
		return n.replace(/0/g,'零').replace(/1/g,'一').replace(/2/g,'二').replace(/3/g,'三').replace(/4/g,'四')
				.replace(/5/g,'五').replace(/6/g,'六').replace(/7/g,'七').replace(/8/g,'八').replace(/9/g,'九');
	}
	// 数字日转汉字日
	function nongN2R(num) {
		var riZh = '零一二三四五六七八九十';
		if (num >= 1 && num <= 10) {
			return '初' + riZh.charAt(num);
		} else if (num >= 10 && num <= 19) {
			return '十' + riZh.charAt(num - 10);
		} else if (num >= 20 && num <= 29) {
			return '廿' + riZh.charAt(num - 20);
		} else if (num == 30) {
			return '三十'
		} else {
			return '某';
		}
	}
	// 根据农历年和月查天数
	function nongMday() {
		var nongY = nongY2N($('[name="nongY"]').val());
		var nong = `${nongY}-${$('[name="nongM"]').val()}-`;
		$.get('/calendar/nong/day?nong=' + nong, function (data) {
			var count = data ? parseInt(data) : 0;
			if (count > 0) {
				$("#nongD").empty();
				for (var i = 1; i <= count; i++) {
					$("#nongD").append(`<option value="${i}" ${i==1?'selected':''}>${nongN2R(i)}</option>`);
				}
				$("#nongD").selectpicker('refresh');
				nong2gong();
			} else {
				alert('错误的日期');
			}
		})
	}
	// 根据农历年月日，转公历
	function nong2gong() {
		var nongY = nongY2N($('[name="nongY"]').val());
		$(this).select();
		var nong = `${nongY}-${$('[name="nongM"]').val()}-${$('[name="nongD"]').val()}`;
		$.get('/calendar?nong=' + nong, function (data) {
			var arr = data.gong.split('-');
			$('[name="gongY"]').val(arr[0]);
			$('[name="gongM"]').val(arr[1]);
			$('[name="gongD"]').val(arr[2]);
			$('[name="gongW"]').val(data.gongStr.slice(-3));
			$('[name="nian"]').val(data.nian);
		})
	}
	$(function () {
		$('[name="nongY"]').on('focus', function () {
			$(this).val(nongY2N($(this).val()));
			$(this).select();
			return false;
		})
		$('[name="nongY"]').on('blur', function () {
			$(this).val(nongN2Y($(this).val()));
			return false;
		})
		$("#nongM").selectpicker({ width: 80});
		$("#nongD").selectpicker({ width: 68});
		$('[name="nongY"]').on('change', nongMday);
		$('[name="nongM"]').on('change', nongMday);
		$('[name="nongD"]').on('change', nong2gong);
		$("#fid").selectpicker({ width: 238, liveSearch: true,liveSearchPlaceholder: "输入姓名搜索..." });
		$("#mid").selectpicker({ width: 238, liveSearch: true,liveSearchPlaceholder: "输入姓名搜索..." });
		$("#xid").selectpicker({ width: 238, liveSearch: true,liveSearchPlaceholder: "输入姓名搜索..." });
		$('[aria-label="Search"]').on('input', function () {
			var id = '#' + $(this).parents('.bootstrap-select').children('select').attr('id');
			$.post('/person/find', {words: $(this).val()}, function (data) {
				console.log(JSON.stringify(data));
				$(id).empty();
				$(id).append('<option value="">没有选中任何项</option>');
				for (let i = 0; i < data.length; i++) {
					$(id).append(`<option value="${data[i].id}">${data[i].name} ${data[i].sex} ${data[i].age}岁 ${data[i].nongStr}</option>`)
				}
				$(id).selectpicker('refresh');
			})
		})
		$('#form_person').on('submit', function () {
			var nongY = nongY2N($('[name="nongY"]').val());
			var nongM = $('[name="nongM"]').val();
			var nongD = $('[name="nongD"]').val();
			$('[name="nong"]').val(`${nongY}-${nongM}-${nongD}`);
			var gongY = $('[name="gongY"]').val();
			var gongM = $('[name="gongM"]').val();
			var gongD = $('[name="gongD"]').val();
			$('[name="gong"]').val(`${gongY}-${gongM}-${gongD}`);
			$.post('/person/save', $(this).serialize(), function (data) {
				if (typeof data == 'string') {
					alert(data);
				} else {
					loadView('/person', {page: '<%=page%>', words: '<%=words%>'});
				}
			})
			return false;
		})
		$('[type="cancel"]').on('click', function () {
			loadView('/person', {page: '<%=page%>', words: '<%=words%>'});
			return false;
		})
	})
</script>